package leo.weixin.service.impl;

import com.baidu.aip.ocr.AipOcr;
import leo.weixin.config.ApiConfig;
import leo.weixin.entity.menu.EventButton;
import leo.weixin.entity.message.BaseMessage;
import leo.weixin.entity.message.TextMessage;
import leo.weixin.service.IEventService;
import leo.weixin.service.IWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("iEventService")
public class EventServiceImpl implements IEventService {
    @Autowired
    private ApiConfig config;
    @Autowired
    private IWxService iWxService;

    // 用于返回
    private BaseMessage message;

    @Autowired
    private AipOcr ocrClient;

    //用于接受事件码
    private String eventKey = null;


    private void dealClick(Map<String, String> map) {
        String eventKey = map.get("EventKey");  //点击菜单的事件码
        // todo 封装eventKey
        switch (eventKey) {
            case "click_support" :
                message = new TextMessage(map,"你已支持");
                break;
            case "click_login" :
                String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APP_ID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                url = url.replace("APP_ID",config.getWxPubAppId())
                        .replace("REDIRECT_URI","https://www.baidu.com"); //授权登录请求地址  这里写你的授权登录控制器地址
                message = new TextMessage(map,"点击这里<a href=" + "'" + url + "'" +">登录</a>");
                break;
        }
    }
    @Override
    public BaseMessage dealEvent(Map<String, String> map) {
        String event = map.get("Event");  //包括菜单按钮事件和 其他(关注,取消关注等)
        eventKey = map.get("EventKey");  // 自定义的 key
        //pic_photo_or_album
        switch (event) {
            case "CLICK" :
                dealClick(map);
                break;
            case "subscribe" : //未关注 扫描关注二维码
                // 扫码关注后进行事件推送
                break;
            case "VIEW" :
                dealView(map);
                break;
            case EventButton.EventButtonType.PIC_PHOTO_OR_ALBUM :
                dealPhoto(map);
                break;
        }
        System.out.println(message);
        return message;
    }

    private void dealPhoto(Map<String, String> map) {
        switch (eventKey) {
            case "event_imageRecognition" :  //图像识别
                message = new TextMessage(map,"正在处理...");
                break;
        }
    }


    public void dealView(Map<String, String> map) {
    }

    private String recImage(String url){
        return null;
    }
}
